<template>
  <div class="cart">
    <!-- 头 -->
    <div class="head">
      <p>购物车</p>
      <span class="delete" @click="deleteGoods">删除</span>
    </div>


    <!-- 列表 -->
    <div class="list" v-if="cartList.length > 0">
      <div v-for="(item, index) in cartList" :key="index" style="display: flex">
        <van-checkbox v-model="checked[index]" />
        <momentStrip :pieceData="item" />
      </div>
    </div>
    <div v-else>
      <van-empty description="你的购物车空空如也~" />
    </div>

    <!-- 总价 -->
    <van-submit-bar :price="getTotal" button-text="结算" @submit="onSubmit">
      <van-checkbox v-model="checkedAll" @click="selectAll">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
import momentStrip from "@/components/momentStrip";
import { SubmitBar, Toast } from "vant";
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  components: {
    momentStrip,
    [SubmitBar.name]: SubmitBar,
  },

  data() {
    return {
      checked: [false, false, false, false, false],
      checkedAll: false,
      cartList: [],
      total: 0,
      myID: 0
    };
  },

  created() {
    this.myID = parseInt(localStorage.getItem("userID"))
    this.getUserAddress({ userID: this.myID });
    this.$axios
      .get(`/getCart?userID=${localStorage.getItem("userID")}`)
      .then((res) => {
        if (res.code === 200) {
          this.cartList = res.data;
        }
      });

  },

  computed: {
    ...mapState(["userAddress"]),
    getTotal() {
      this.total = 0;
      this.checked.forEach((item, index) => {
        if (item === true) {
          this.total += this.cartList[index].price;
        }
      });
      return this.total * 100;
    },
  },

  methods: {
    ...mapActions(["getUserAddress"]),
    ...mapMutations(["SET_ORDER"]),
    selectAll() {
      if (this.checkedAll === true) {
        for (let i = 0; i < this.cartList.length; i++) {
          this.$set(this.checked, i, true);
        }
      } else {
        for (let i = 0; i < this.cartList.length; i++) {
          this.$set(this.checked, i, false);
        }
      }
    },

    deleteGoods() {
      let deleteList = []
      this.checked.forEach((item, index) => {
        if (item === true) {
          deleteList.push(this.cartList[index].momentID);
        }
      });
      this.$axios.post('/deleteGoods', { myid: this.myID, goods: deleteList }).then(res => {
        if (res.code === 200) {
          this.cartList.forEach((item, index) => {
            if (deleteList.includes(item.momentID)) {
              this.cartList.splice(index, 1)
              this.checked[index] = false
            }
          })
        }
      })
    },
    onSubmit() {
      let order = [];
      if (this.total === 0) {
        Toast("你还没选择宝贝哦");
      } else {
        if (this.userAddress.length===0) {
          this.$router.push("/setAddress")
        } else {
          this.checked.forEach((item, index) => {
            if (item === true) {
              order.push(this.cartList[index].momentID);
            }
          });
          this.$axios.post('/lockMoment', { 'momentID': order }).then(res => {
            if (res.code === 200) {
              this.SET_ORDER(res.data);
              let orderD = []
              this.checked.forEach((item, index) => {
                if (item === true) orderD.push(this.cartList[index])
              })
              this.$router.push({ name: "settle", params: { orderD: orderD } });
            } else {
              Toast(res.msg)
            }
          })
        }
      }
    },
  },
};
</script>

<style scoped>
.list {
  text-align: left;
}

.head {
  height: 50px;
}

.head>p {
  font-weight: bold;
  font-size: 16px;
  margin: 0;
  line-height: 50px;
}

.address {
  width: 100vw;
  font-size: 14px;
  background-color: rgb(233, 136, 10, 0.2);
}

.van-submit-bar {
  bottom: 13.333vw;
}

.delete {
  color: skyblue;
  font-size: 14px;
  position: absolute;
  right: 10px;
  top: 20px
}
</style>